//
//  FixSystemBug.m
//  MasterBoy
//
//  Created by kyson on 2023/10/9.
//

#import "FixSystemBug.h"


#import <dlfcn.h>
#import <UIKit/UIKit.h>


void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) {
    if (size.width <= 0 || size.height <= 0) {
        return;
    }
    static void * uikit = NULL;
    static void (*realFunc)(CGSize size, BOOL opaque, CGFloat scale) = NULL;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class UIApplicationClass = NSClassFromString(@"UIApplication");
        NSBundle *uikitBundle = [NSBundle bundleForClass:UIApplicationClass];
        uikit = dlopen(uikitBundle.executablePath.UTF8String, RTLD_NOW);
        if (uikit) {
            realFunc = dlsym(uikit, "UIGraphicsBeginImageContextWithOptions");
        }
    });
    if (realFunc) {
        realFunc(size, opaque, scale);
    }
    
}


void UIGraphicsBeginImageContext(CGSize size) {
    if (size.width <= 0 || size.height <= 0) {
        return;
    }
    static void * uikit = NULL;
    static void (*realFunc)(CGSize size) = NULL;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class UIApplicationClass = NSClassFromString(@"UIApplication");
        NSBundle *uikitBundle = [NSBundle bundleForClass:UIApplicationClass];
        uikit = dlopen(uikitBundle.executablePath.UTF8String, RTLD_NOW);
        if (uikit) {
            realFunc = dlsym(uikit, "UIGraphicsBeginImageContext");
        }
    });
    if (realFunc) {
        realFunc(size);
    }
}

